package com.itrident.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.impl.file.PsiJavaDirectoryImpl;
import com.itrident.data.DataSource;
import com.itrident.dialog.ItridentDiglog;

public class ToolsAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        DataSource.initData ();
        PsiJavaDirectoryImpl node = (PsiJavaDirectoryImpl)e.getRequiredData (CommonDataKeys.NAVIGATABLE);
        String packagePath = JavaDirectoryService.getInstance().getPackage(node).getQualifiedName ();
        VirtualFile virtualFile = node.getVirtualFile ();
        String path = virtualFile.getPath ();
        ItridentDiglog diglog = new ItridentDiglog (virtualFile,packagePath,path);
        diglog.showAndGet ();
    }

    @Override
    public void update(AnActionEvent e) {
        if (!isJavaDir (e)){
            e.getPresentation().setEnabled (false);
        }
    }

    private boolean isJavaDir(AnActionEvent e){

        try {
            Navigatable navigatable = e.getRequiredData (CommonDataKeys.NAVIGATABLE);
            return navigatable instanceof PsiJavaDirectoryImpl
                    && JavaDirectoryService.getInstance().getPackage((PsiJavaDirectoryImpl) navigatable) != null;
        } catch (Exception e1) {
        }
        return false;
    }

}
